package com.icxcc.cms.config;

import com.icxcc.constant.RabbitConstant;
import com.google.common.collect.Maps;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class RabbitConfig {

    /**
     * 创建消息交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(RabbitConstant.DEFAULT_EXCAHNGE, true, false);
    }

    /**
     * 创建死信队列
     * @return
     */
    @Bean
    public Queue delayDeadLetterQueue(){
        Map<String, Object> arguments = Maps.newHashMap();
        arguments.put("x-dead-letter-exchange",RabbitConstant.DEFAULT_EXCAHNGE);
        arguments.put("x-dead-letter-routing-key",RabbitConstant.DELAY_MESSAGE_QUEUE);
        return QueueBuilder.durable(RabbitConstant.DELAY_DEAD_LATTER_QUEUE).withArguments(arguments).build();
    }
    /**
     * 创建延时消息队列
     * @return
     */
    @Bean
    public Queue delayMessageQueue(){
        return QueueBuilder.durable(RabbitConstant.DELAY_MESSAGE_QUEUE).build();
    }
    /**
     * 创建延时消息处理队列
     * @return
     */
    @Bean
    public Queue delayProcessQueue(){
        return QueueBuilder.durable(RabbitConstant.DELAY_PROCESS_QUEUE).build();
    }

    @Bean
    Binding delayMessageBinding(){
        return BindingBuilder.bind(delayMessageQueue()).to(directExchange()).with(RabbitConstant.DELAY_MESSAGE_QUEUE);
    }

    @Bean
    Binding delayDeadLetterBinding(){
        return BindingBuilder.bind(delayDeadLetterQueue()).to(directExchange()).with(RabbitConstant.DELAY_DEAD_LATTER_QUEUE);
    }

    @Bean
    Binding delayProcessBinding(){
        return BindingBuilder.bind(delayProcessQueue()).to(directExchange()).with(RabbitConstant.DELAY_PROCESS_QUEUE);
    }
}
